package com.wutong.eams.utils;

import org.mindrot.jbcrypt.BCrypt;

import java.util.Arrays;

public class StringUtil {

    public static String convertUnderline(String camelCase) {
        StringBuilder result = new StringBuilder();
        if (camelCase != null && !camelCase.isEmpty()) {
            result.append(camelCase.substring(0, 1).toLowerCase());
            for (int i = 1; i < camelCase.length(); i++) {
                char c = camelCase.charAt(i);
                if (Character.isUpperCase(c)) {
                    result.append("_");
                    result.append(Character.toLowerCase(c));
                } else {
                    result.append(c);
                }
            }
        }
        return result.toString();
    }

    public static String convertCamel(String underline) {
        StringBuilder result = new StringBuilder();
        if (underline != null && !underline.isEmpty()) {
            result.append(underline.substring(0, 1).toLowerCase());
            for (int i = 1; i < underline.length(); i++) {
                char c = underline.charAt(i);
                if (c == '_') {
                    i++;
                    result.append(Character.toUpperCase(underline.charAt(i)));
                } else {
                    result.append(c);
                }
            }
        }
        return result.toString();
    }

    public static String[] stringListBuilder(String str, int length) {
        String[] result = new String[length];
        Arrays.fill(result, str);
        return result;
    }

    public static String encrypt(String raw) {
        return BCrypt.hashpw(raw, BCrypt.gensalt());
    }

    public static boolean check(String raw, String encrypted) {
        return BCrypt.checkpw(raw, encrypted);
    }

}
